<?php
App::uses('AppController', 'Controller');
class NewsController extends AppController {

	var $controllerName = "Quản lý bài viết";

	public function beforeFilter() {
        parent::beforeFilter();
    }
	public function index($id = null) {
		if($id == null){
			$cons = array('News.home'=>1);
		}else $cons = array('News.id'=>$id);
		$hienthi = $this->News->find('first',array('conditions'=>$cons));
		$data = $this->News->find('all',array('fields'=>array('id','name','home','content'),'conditions'=>array('News.status'=>2)));
		$this->set(compact('data','hienthi'));
		//debug($data);
		//debug($this->request->params['controller']);
	}	
	
	public function hienthi($id = null){
		$this->autoRender = false;
		$hienthi = $this->News->find('first',array('conditions'=>array('News.status' => 2,'News.home' => 1)));
		$this->set(compact('hienthi'));
	}
	public function view($id = null) {
		if (!$this->News->exists($id)) {
			throw new NotFoundException(__('Invalid news'));
		}
		$options = array('conditions' => array('News.' . $this->News->primaryKey => $id));
		$this->set('news', $this->News->find('first', $options));
	}
	public function add(){
		//$this->set('saved', false); //false by default - controls closure of overlay in which this is opened  
		if ($this->request->is('post')) {
			//			
			$this->News->create();
			if ($this->News->save($this->request->data)) {
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Chưa lưu được. Hãy thử lại.'));
			}
			
			
		}		
	}
	public function admin_add(){
	
	}
	
	public function edit($id = null){}
}
